package top.jacktgq.itheima.netty.sticking_wrapping.resolve.fixedLength;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.Random;

/**
 * @Author CandyWall
 * @Date 2021/3/23--16:49
 * @Description 客户端
 */
@Slf4j
public class Client {
    public static byte[] fill10Bytes(char c, int len) {
        byte[] bytes = new byte[10];
        int i = 0;
        for (; i < len; i++) {
            // System.out.print(c);
            bytes[i] = (byte) c;
        }
        for (; i < 10; i++) {
            // System.out.print('_');
            bytes[i] = '_';
        }
        // System.out.println();
        return bytes;
    }

    public static void main(String[] args) {
        Random random = new Random(10);
        NioEventLoopGroup group = new NioEventLoopGroup();
        ChannelFuture channelFuture = new Bootstrap()
                .group(group)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<NioSocketChannel>() {
                    @Override
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG)).addLast(new ChannelInboundHandlerAdapter() {
                            @Override
                            /**
                             * 连接建立成功时就调用该方法 等价于 给channelFuture添加监听事件的方式
                             */
                            public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                ByteBuf buffer = ctx.alloc().buffer();
                                char c = '0';
                                for (int i = 0; i < 10; i++) {
                                    buffer.writeBytes(fill10Bytes(c, random.nextInt(10) + 1));
                                    c++;
                                }
                                ch.writeAndFlush(buffer);
                                super.channelActive(ctx);
                            }

                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {


                                super.channelRead(ctx, msg);
                            }
                        });
                    }
                })
                .connect("localhost", 8080);
        channelFuture.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                log.debug("客户端连接成功...");
                Channel channel = channelFuture.channel();
                channel.closeFuture().addListener(new ChannelFutureListener() {
                    @Override
                    public void operationComplete(ChannelFuture future) throws Exception {
                        log.debug("服务器正在关闭...");
                        group.shutdownGracefully();
                    }
                });
            }
        });
    }
}
